package com.AUI.components
{
	import com.AUI.*;
	import com.AUI.events.*;
	
	import flash.events.*;
	/**
	 * @eventType com.AUI.events.AGroupButtonEvent.SELECTED
	 */
	[Event(name="selected", type="com.AUI.events.AGroupEvent")]
	/**
	 * Group Button,mutex in the group of same number. 
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class GroupButton extends Button
	{
		/**
		 * Group number.
		 */
		private var _number:uint = 0;
		/**
		 * Constructor.
		 * @param number The group number of the button,must be less than 2147483648(0x80000000),the values above 0x7FFFFFFF are system reserve. 
		 * @param data The label of the button.
		 */
		public function GroupButton(number:uint = 0,data:String = '')
		{
			// init attributes
			super(data);
			_number = number;
			// init events
			AUIPrivate.ED.addEventListener(AGlobalEvent.GROUP_ + _number,onGroup,false,0,true);

			/**************** INITCOMPLETE ****************/
			Object(this).constructor == GroupButton && initComplete();
		}
		/**
		 * The group number of the button.
		 */
		public function get number():int
		{
			return _number;
		}
		/**
		 * @private
		 */
		override protected function onClick(e:MouseEvent):void
		{
			checked ? updateStyle() : (checked = true);
		}
		/**
		 * @private
		 */
		private function onGroup(e:AGlobalEvent):void
		{
			var button:GroupButton = GroupButton(e.source);
			if(button == this)
			{
				return;
			}
			super.checked = false;
		}
		/**
		 * @private
		 */
		override public function set checked(value:Boolean):void
		{
			super.checked = value;
			if(value)
			{
				var e1:AGroupButtonEvent = new AGroupButtonEvent(AGroupButtonEvent.SELECTED);
				dispatchEvent(e1);
					
				var e2:AGlobalEvent = new AGlobalEvent(AGlobalEvent.GROUP_ + _number);
				e2.source = this;
				AUIPrivate.ED.dispatchEvent(e2);
			}
		}
	}
}